$(function(){
        // 创建 URLSearchParams 对象
        var params = new URLSearchParams(window.location.search);
        // 获取参数值
        var h3Content = params.get('h3Content');
        var productId = params.get('ID');
        // 设置商品的名字
        $(".goodsname").text(h3Content);
        // 发送请求 获取数据
        $.ajax({
                url:"http://127.0.0.1:8080/product/data/details/",
                type:"GET",
                data:{
                        productId:productId
                },success:function(response){
                        $("#goosdcategoryies").text(response.data.product.categories);
                        $("#goosdtags").text(response.data.product.tags);
                        $("#imgurlself").attr("src",response.data.product.imageUrl)
                        // console.log(response);
                        var recs = response.data.productRecs.recs;
                        show_list_product(recs);

                }
        })

        function show_list_product(recs){
                // console.log(recs)
                $.each(recs,function(index,value){
                        var productId = recs[index].productId;
                        var score = recs[index].score;
                        var li = '<div class="col-sm-12"><div class="thumbnail"><img class="img_text_center" id="img-'+productId+'" src="..." alt="..."><div class="caption"><h3 id="name-'+productId+'"></h3><p id="score-'+productId+'">'+score+'</p><p><a href="#" id="btn-'+productId+'" class="btn btn-primary" role="button">进入详情</a> </p></div></div></div>'
                        $(".row").append(li)
                        show_list_img_name(productId)
                })
        }

        function show_list_img_name(productId){
                // console.log(productId)
                $.ajax({
                        url:"http://127.0.0.1:8080/product/data/",
                        type:"GET",
                        data:{
                                productId:productId
                        },
                        success: function (response){
                                // console.log(response)
                                var name = response.data.name;
                                var imgurl = response.data.imageUrl;
                                $("#name-"+productId).text(name)
                                $("#img-"+productId).attr("src", imgurl)
                                // resolve(name);

                        }
                })

        }


        $(document).on("click", ".btn-primary", function() {
                var h3Content = $(this).closest(".thumbnail").find("h3").text();
                var buttonId = $(this).attr("id");
                var ID = buttonId.substring(4);

                // 重定向到登陆界面
                window.location.href = "./goodsdetails.html?h3Content="+h3Content+"&ID="+ID;
        });
})